/**
 * Created by admin on 2017/5/19.
 */

import {
  ChubIndex,
  ChubAct,
  ChubDetail,
  ChutCommentsList,
  ChutGoods
} from '../mutation-types';
import {setPageTitle} from '../../utils/util.js';
import {  Cache, cache_keys } from '../../utils/util';
export const queryChubDetail = function ({commit}, id) {
  let remove=EventBus.$Message.loading({text:"页面加载中"});
  EventBus.$http.get(EventBus.$Config.baseUrl+ 'api/admin/cms/contenthub/detail/'+id).then(res=>{
    // EventBus.$Loading.done();
    if(res.status){
      EventBus.title={
        1:'测评详情',
        2:'指南详情'
      }[res.data.data.content_type];
      setPageTitle(EventBus.title);
      commit(ChubDetail, res.data.data);
      EventBus.$emit('set-title', EventBus.title);
      remove();
    }
    else{
      remove();
      EventBus.$Modal.info({title:"消息提示",body:res.data.message || '未知错误'});
    }
  },()=>{
    remove();
    EventBus.$Modal.info({title:"消息提示",body:'加载失败'});
  });
};
export const queryChubList = function ({commit},data) {
  console.log(data);
  let content_type=data.number || 1;
  let index =data.type || 0;
  let i=data.index;
  let page=data.page || 1;
  let title = {
    1: '评测',
    2: '指南',
    3: '活动'
  }[content_type];
  setPageTitle(title);
   EventBus.$emit("set-title",title);
  // const activity_type = this.tabList[index].activity_type;
   const activity_type= index;

   EventBus.$http.get(EventBus.$Config.baseUrl +'api/admin/cms/contenthub/articles/' + content_type +'/'+ activity_type +'/' + page ).then(res => {
   console.log(res.data);
    if(res.data.status){
      EventBus.$emit('complete-chubList', res.data, i,data.method);
    }else{
      EventBus.$Modal.info( {body:res.data.message || '未知错误'});
    }

  }, () => {
    // this.$Loading.done();
    EventBus.$Modal.info({body:'加载失败'});
  })
};
export const queryChubFavoState = function ({commit}, id) {

  // 拿自己本地
  const oauth = Cache.get(cache_keys.token);
  //
  EventBus.$http.get(EventBus.$Config.baseUrl+ 'api/admin/cms/contenthub/collect/check/' + id , {
    headers: {
      Authorization: oauth.access_token
    }
  }).then(res => {
    // res = res.json();
    EventBus.$emit('complete-queryFavo', res.data.status);

  },() => {
    EventBus.$Modal.info({body:'加载失败'});
  })
};


export const submitChutFavo = function ({commit},data) {
  const oauth = Cache.get(cache_keys.token);
  EventBus.$http.post(EventBus.$Config.baseUrl+ 'api/admin/cms/contenthub/collect', {
    article_id: data.id,
    action: data.action
  }, {
    headers: {
      Authorization: oauth.access_token
    }
  }).then(res => {
    if(res.data.status){
      EventBus.$emit('complete-submitFavo', res.data.data.status);
    }else{
      EventBus.$Modal.info( {body:res.data.message || '收藏失败'});
    }

  },() => {
    EventBus.$Modal.info( {body: '加载失败'});
  })
};

export const queryChutGoods =function({commit},article_id){
    let remove=EventBus.$Message.loading({text:"页面加载中"});
    EventBus.$http.get(EventBus.$Config.baseUrl+'api/admin/cms/contenthub/detailed/list/'+article_id).then( res=>{
       if(res.data.status){
          commit(ChutGoods,res.data.data);
          remove();
       }
       else{
         EventBus.$Modal.info( {body:res.data.message || '未知错误'});
         remove();
       }
    },()=>{
       remove();
      EventBus.$Modal.info( {body:'加载失败'});
    });
}

export const queryChutCommentsList =function({commit,state},datas){
   let article_id=datas.id;
   let page=datas.page || 1;
    EventBus.$http.get(EventBus.$Config.baseUrl+'api/admin/cms/contenthub/comment/list/'+article_id+'?page='+page).then(res=>{
      if(res.data.status){
         let data =res.data.data;
         if(datas.page>1)
         {
            data =state.chutCommentsList.concat(data);
         }
         commit(ChutCommentsList,data);
         if(res.data.meta && res.data.meta.pagination)
         {
             EventBus.$emit('complete-page',true,res.data.meta.pagination,data.method);
         }
         else{
             EventBus.$emit('complete-page',false,"",datas.method);
        }
      }
      else{
        EventBus.$Modal.info( {body:res.data.message || '未知错误'});
      }
    },()=>{
        EventBus.$Modal.info( {body:'加载失败'});
    })
}

export const submitChutComments =function({commit},datas){
   const oauth =Cache.get(cache_keys.token);
   EventBus.$http.post(EventBus.$Config.baseUrl+'api/admin/cms/contenthub/comment',{
     article_id:datas.article_id,
     comment:datas.comment
   },{
     headers: {
       Authorization: oauth.access_token
     }
   }).then(res =>{
      if(res.data.status){
          EventBus.$emit('complete-submit-comment');
      }
      else{
         EventBus.$Modal.info({body:res.data.message || '评论失败'});
      }
   },()=>{
     EventBus.$Modal.info({body:'加载失败'});
   })
}

export const queryChutActivityType =function({commit,state}){
    EventBus.$http.get(EventBus.$Config.baseUrl+'api/admin/cms/contenthub/activity/type/list').then(res =>{
       if(res.data.status){
         EventBus.$emit('complete-activity-type',res.data.data);
       }
       else{
         EventBus.$Modal.info({title:'消息提示',body:res.data.message || "未知错误"});
       }
    },()=>{
        EventBus.$Modal.info({title:'消息提示',body:'加载失败'});
    });
}


